package com.example.redis.handle;

import com.example.redis.callback.LockRedisCallback;
import com.example.redis.callback.UnLockRedisCallback;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * 加锁、解锁处理器
 *
 * @author xianxin
 * @since 2020/8/21
 */
@Slf4j
public class RedisDistributedLockHandle extends AbstractRedisDistributedLockHandle {

    private RedisTemplate<String, String> redisTemplate;

    public RedisDistributedLockHandle(RedisTemplate<String, String> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @Override
    public Boolean lock(String lockKey, String lockValue, long lockExpireTime) {
        LockRedisCallback callback = new LockRedisCallback();
        callback.setLockKey(lockKey);
        callback.setLockValue(lockValue);
        callback.setLockExpireTime(lockExpireTime);

        Boolean result = redisTemplate.execute(callback);
        return result;
    }

    @Override
    public Boolean unlock(String lockKey, String lockValue) {
        UnLockRedisCallback callback = new UnLockRedisCallback();
        callback.setLockKey(lockKey);
        callback.setLockValue(lockValue);

        Boolean result = redisTemplate.execute(callback);
        return result;
    }
}
